package com.ztc.ZtcRouteAttachment.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.aqgj.util.BaseDaoImpl;
import com.ztc.ZtcRouteAttachment.bean.ZtcRouteAttachment;

@Repository("ZtcRouteAttachmentDao")
public class ZtcRouteAttachmentDao extends BaseDaoImpl<ZtcRouteAttachment> {
    public String save(ZtcRouteAttachment ztcRouteAttachment) {
        this.getHibernateTemplate().save(ztcRouteAttachment);
        return ztcRouteAttachment.getId();
    }

    public List<ZtcRouteAttachment> getAllZtcRouteAttachments() {
        return this.getAll();
    }

    public String delete(String id, String routeId) {
        String sql = "delete from ztc_route_attachment where 1=1";
        if (id != null) {
            sql += " and id='" + id + "'";
        }
        if (routeId != null) {
            sql += " and fk_ztc_route_id='" + routeId + "'";
        }
        if (this.currentSession().createNativeQuery(sql).executeUpdate() >= 0) {
            return "1";
        }
        return "0";
    }

    public List<ZtcRouteAttachment> searchInfoEquals(ZtcRouteAttachment ztcRouteAttachment) {
        String sql = "select * from ztc_route_attachment where 1=1";
        if (ztcRouteAttachment.getViewContent() != null) {
            sql += " and view_content='" + ztcRouteAttachment.getViewContent() + "'";
        }
        if (ztcRouteAttachment.getFkAttachmentId() != null) {
            sql += " and fk_attachment_id='" + ztcRouteAttachment.getFkAttachmentId() + "'";
        }
        if (ztcRouteAttachment.getSequence() != null) {
            sql += " and sequence='" + ztcRouteAttachment.getSequence() + "'";
        }
        if (ztcRouteAttachment.getFkZtcRouteId() != null) {
            sql += " and fk_ztc_route_id='" + ztcRouteAttachment.getFkZtcRouteId() + "'";
        }
        if (ztcRouteAttachment.getType() != null) {
            sql += " and type='" + ztcRouteAttachment.getType() + "'";
        }
        if (ztcRouteAttachment.getId() != null) {
            sql += " and id='" + ztcRouteAttachment.getId() + "'";
        }
        sql+="order by group_number ";
        List<ZtcRouteAttachment> list = this.currentSession().createNativeQuery(sql,ZtcRouteAttachment.class)
                .getResultList();
        return list;
    }

    public List<ZtcRouteAttachment> searchInfoLikes(String viewContent) {
        String sql = "select * from ztc_route_attachment  where 1=1";
        if (viewContent != null) {
            sql += " and view_content like '%" + viewContent + "%'";
        }
        List<ZtcRouteAttachment> list = this.currentSession().createNativeQuery(sql,ZtcRouteAttachment.class)
                .getResultList();
        return list;
    }
	public String update(ZtcRouteAttachment ztcRouteAttachment) {
		ZtcRouteAttachment ztcRouteAttachmentBean = this.findUniqueBy("id", ztcRouteAttachment.getId());
		if (ztcRouteAttachment.getFkAttachmentId()!= null) {
			ztcRouteAttachmentBean.setFkAttachmentId(ztcRouteAttachment.getFkAttachmentId());
		}
		if (ztcRouteAttachment.getViewContent() != null) {
			ztcRouteAttachmentBean.setViewContent(ztcRouteAttachment.getViewContent());
		}
		if (ztcRouteAttachment.getSequence() != null) {
			ztcRouteAttachmentBean.setSequence(ztcRouteAttachment.getSequence());
		}
		if (ztcRouteAttachment.getFkZtcRouteId() != null) {
			ztcRouteAttachmentBean.setFkZtcRouteId(ztcRouteAttachment.getFkZtcRouteId());
		}
		this.save(ztcRouteAttachmentBean);
		if (ztcRouteAttachmentBean.getId() != null) {
			return "1";
		}
		return "0";
	}
}